package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.LinkMapper;
import com.itheima.pojo.Link;
import com.itheima.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkMapper linkMapper;
    @Override
    @Transactional
    public int save(Map linkVO) {
        Link link = new Link();
        link.setUid((String)linkVO.get("uid"));
        link.setUrl((String)linkVO.get("url"));
        link.setClickCount(0L);
        link.setCreateTime(new Date());
        link.setUpdateTime(new Date());
        link.setLinkStatus((Integer)linkVO.get("linkStatus"));
        Integer sort = (Integer) linkVO.get("sort");
        link.setSort(sort.longValue());
        link.setSummary((String)linkVO.get("summary"));
        link.setTitle((String)linkVO.get("title"));
        linkMapper.insertSelective(link);
        return 1;
    }

    @Override
    public int delete(Map linkVO) {
        linkMapper.deleteByPrimaryKey((String)linkVO.get("uid"));
        return 1;
    }

    @Override
    public Map<String,Object> getList(Map linkVO) {
        Integer page = (Integer)linkVO.get("currentPage");
        Integer size = (Integer)linkVO.get("pageSize");
        PageHelper.startPage(page, size);
        Example example = new Example(Link.class);
        String keyword = (String)linkVO.get("keyword");
        Example.Criteria criteria = example.createCriteria();
        if (keyword!=null && !"".equals(keyword)){
            criteria.andEqualTo("title",keyword);
        }
        Page<Link> links = (Page<Link>)linkMapper.selectByExample(example);
        Map<String,Object> map = new HashMap();
        map.put("current",page);
        map.put("records",links.getResult());
        map.put("size",size);
        map.put("orders",new ArrayList<>());
        map.put("total",links.getTotal());
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return map;
    }

    @Override
    public int update(Map linkVO) {
        Link link1 = linkMapper.selectByPrimaryKey((String) linkVO.get("uid"));
        Link link = new Link();
        link.setUid((String)linkVO.get("uid"));
        if (linkVO.get("url")!=null && !"".equals((String)linkVO.get("url"))){
            link.setUrl((String)linkVO.get("url"));
        }
        link.setClickCount(link1.getClickCount() + 1);
        if (linkVO.get("linkStatus")!=null){
            link.setLinkStatus((Integer)linkVO.get("linkStatus"));
        }
        if (linkVO.get("sort")!=null) {
            Integer sort = (Integer) linkVO.get("sort");
            link.setSort(sort.longValue());
        }
        if (linkVO.get("summary")!=null && !"".equals((String)linkVO.get("summary"))) {
            link.setSummary((String)linkVO.get("summary"));
        }
        if (linkVO.get("title")!=null && !"".equals((String)linkVO.get("title"))) {
            link.setTitle((String)linkVO.get("title"));
        }
        link.setUpdateTime(new Date());
        int i = linkMapper.updateByPrimaryKeySelective(link);
        return i;
    }

    @Override
    public int stick(String uid) {
        int sort = linkMapper.selectmaxsort();
        Link link = linkMapper.selectBysort(sort);
        System.out.println("最大sort:"+sort);
        if (uid.equals(link.getUid())){
            return 2;
        }
        int i = linkMapper.sticklink(uid, sort+1);
        return i;
    }

    @Override
    public boolean addLinkCount(String uid) {
        int i = linkMapper.addLinkCount(uid);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Link> getLink() {
        List<Link> links = linkMapper.selectAll();
        return links;
    }

    public static boolean checkCellphone(String cellphone) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,1,2,5-9])|(177))\\d{8}$";
        Pattern pattern= Pattern.compile(regex);
        Matcher matcher=pattern.matcher(cellphone);
        return matcher.matches();
    }

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Link.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 链接uid
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andEqualTo("uid", searchMap.get("uid"));
            }
            // 连接标题
            if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
                criteria.andLike("title", "%" + searchMap.get("title") + "%");
            }
            // 链接介绍
            if (searchMap.get("summary") != null && !"".equals(searchMap.get("summary"))) {
                criteria.andLike("summary", "%" + searchMap.get("summary") + "%");
            }
            // 链接url
            if (searchMap.get("url") != null && !"".equals(searchMap.get("url"))) {
                criteria.andLike("url", "%" + searchMap.get("url") + "%");
            }
            // 链接状态
            if (searchMap.get("linSktatus") != null && !"".equals(searchMap.get("linSktatus"))) {
                criteria.andEqualTo("linSktatus", searchMap.get("linSktatus"));
            }
            // 操作管理员uid
            if (searchMap.get("adminUid") != null && !"".equals(searchMap.get("adminUid"))) {
                criteria.andEqualTo("adminUid", searchMap.get("adminUid"));
            }
            // 申请用户Uid
            if (searchMap.get("userUid") != null && !"".equals(searchMap.get("userUid"))) {
                criteria.andEqualTo("userUid", searchMap.get("userUid"));
            }
        }
        return example;
    }
}
